package com.vincce.fitness.core.interceptor;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by henry on 5/7/14.
 */
public class WechatBrowserInterceptor extends HandlerInterceptorAdapter {

	private static final Map<String, String> blockUas = new HashMap<String, String>();
	static {
		blockUas.put("iemobile", "No WinPhone!");
	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		if (true) {
			String userAgent = request.getHeader("User-Agent");
			if (userAgent == null
					|| !userAgent.toLowerCase().contains("micromessenger")) {
				throw new RuntimeException("Wechat Browser Only!" + userAgent);
			} else {
				for (Map.Entry<String, String> entry : blockUas.entrySet()) {
					if (userAgent.toLowerCase().contains(entry.getKey())) {
						throw new RuntimeException(entry.getValue());
					}
				}
			}
		}
		return super.preHandle(request, response, handler);
	}
}
